﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Common.DBContexts;
using Traceability.Common.Infrastructure.InterFaces;

namespace Traceability.Common.Infrastructure.Repositories
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        public EFDBContext db;
        public BaseRepository(EFDBContext _db)
        {
            db = _db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddAsync(T entity)
        {
             db.Set<T>().Add(entity);
             return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<T>> GetAll()
        {
            var list = db.Set<T>().AsQueryable();
            return  list;
        }
        /// <summary>
        /// 修改异步
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
            var list = db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAllS()
        {
            var list = db.Set<T>().AsQueryable<T>();
            return list;
        }
    }
}
